package com.softwinner.bt;

import com.softwinner.bt.policy.BTManager;
import com.softwinner.bt.service.BtService;
import com.softwinner.bt.util.DatabaseHelper;
import com.softwinner.bt.util.Utils;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.View.OnClickListener;

public abstract class BaseActivity extends Activity implements OnClickListener {

	PowerManager mPM;
    PowerManager.WakeLock mWakeLock;
    protected BTManager mBTManager;
    protected DatabaseHelper mDatabaseHelper;    
    /**
     * 请求WakeLock锁，防止灭屏
     */
    private void acquire(){
    	if(mWakeLock==null){
    		mPM = (PowerManager) this.getSystemService(Context.POWER_SERVICE);  
            mWakeLock = mPM.newWakeLock(PowerManager.FULL_WAKE_LOCK
					| PowerManager.SCREEN_BRIGHT_WAKE_LOCK
					| PowerManager.ON_AFTER_RELEASE
					| PowerManager.ACQUIRE_CAUSES_WAKEUP, "phone");  
    	}
    	if(!mWakeLock.isHeld()){
    		mWakeLock.acquire();
    	}	
    }
    /**
     * 释放WakeLock锁
     */
    private void release(){
    	if(mWakeLock==null){
    		return;
    	}	
    	if(mWakeLock.isHeld()){
    		mWakeLock.release();
    	}	    		
    }
    /**
     * 接收挂断以及通话状态的广播
     * 其中挂断finish掉Activity
     */
	protected BroadcastReceiver receiver = new BroadcastReceiver() {		
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if(action.equals(Utils.ACTION_HANGUP)){
				finish();
			}else if(action.equals(Utils.ACTION_BTSTATE_CHANGED)){
				if(BtService.mBtStatus==BTManager.STATE_UNCONNECTION){
					finish();
				}
			}else if(action.equals(Utils.ACTION_PHONE_CALL)
					||action.equals(Utils.ACTION_PHONE_COMING)
					||action.equals(Utils.ACTION_PHONE_INCALL)){
				phoneStateChanged(action);
			}
		}
	};
	
	protected abstract void phoneStateChanged(String action);
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mBTManager = BTManager.getInstance();
        mDatabaseHelper = DatabaseHelper.getDatabaseHelper(this);
        acquire();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Utils.ACTION_HANGUP);
        filter.addAction(Utils.ACTION_BTSTATE_CHANGED);
        filter.addAction(Utils.ACTION_PHONE_CALL);
        filter.addAction(Utils.ACTION_PHONE_COMING);
        filter.addAction(Utils.ACTION_PHONE_INCALL);
        registerReceiver(receiver, filter);
    }
    
    public String getName(String num) throws Exception{
    		String name = mDatabaseHelper.getNameByNum(num);
        	if(name.equals("")){
        		return name;
        	} 
    	return Utils.NameForNum(num);
    }
    
    @Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		return;
	}
    
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
    	unregisterReceiver(receiver);
    	release();
        super.onDestroy();
    }
}
